package com.ks.xcamera.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.google.gson.Gson;
import com.ks.basictools.base.BaseActivity;
import com.ks.basictools.manager.ActivityManager;
import com.ks.basictools.utils.ToastUtils;
import com.ks.xcamera.R;
import com.ks.xcamera.constant.Constants;
import com.ks.xcamera.databinding.ActivityLoginBinding;
import com.ks.xcamera.manager.ListDataManager;
import com.ks.xcamera.manager.PreferenceManager;
import com.ks.xcamera.manager.UserManager;
import com.ks.xcamera.responseBean.UserResponse;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends BaseActivity {

    private ActivityLoginBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBar(true, true, R.color.colorTrans);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        mBinding.setActivity(this);
        start();
    }

    private void start() {
        initView();
        initListener();
    }

    private void initView() {
    }

    private void initListener() {
    }

    public void onClick_login(View ignoreV) {
        String account = mBinding.etAccount.getText().toString();
        String password = mBinding.etPassword.getText().toString();
        if (TextUtils.isEmpty(account) || account.length() != 11) {
            ToastUtils.show(this, "请输入正确的手机号码");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtils.show(this, "请输入密码");
            return;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("account", account);
        param.put("password", password);
        XHttp.post(Constants.URL_LOGIN)
                .params(param)
                .execute(new SimpleCallBack<UserResponse>() {
                    @Override
                    public void onSuccess(UserResponse bean) {
                        Log.d("zhxk", "onSuccess: UserBean = " + bean);
                        UserManager.getInstance().saveUser(bean.getUserinfo());
                        String json = new Gson().toJson(bean.getUserinfo());
                        PreferenceManager.getInstance().setToken(LoginActivity.this, bean.getUserinfo().getToken());
                        PreferenceManager.getInstance().setUser(LoginActivity.this, json);

                        ListDataManager.getInstance().requestData();
                        startActivity(new Intent(LoginActivity.this, BarCodeActivity.class));
                        ActivityManager.getInstance().finishActivity(LoginActivity.class);
                    }

                    @Override
                    public void onError(ApiException e) {
                        e.printStackTrace();
                        ToastUtils.show(LoginActivity.this, e.getMessage());
                    }
                });
    }
}